package com.swt.gap.controller;


import com.swt.gap.model.entity.ModelClass;
import com.swt.gap.service.ModelClassService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author sx
 */
@Api(tags = "模型分类")
@RestController
@RequestMapping("/model-class")
@Slf4j
public class ModelClassController {

    private final ModelClassService modelClassService;

    public ModelClassController(ModelClassService modelClassService) {
        this.modelClassService = modelClassService;
    }

    @PostMapping
    @ApiOperation(value = "创建模型分类", notes = "创建模型分类")
    @ApiImplicitParam(paramType = "body", name = "modelClass", dataType = "ModelClass", value = "ModelClass")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED", response = ModelClass.class)})
    public ResponseEntity saveModelClass(@RequestBody ModelClass modelClass) {
        int userId = UserLoginUtils.getUserId();
        modelClass.setCreatedBy(userId);
        modelClass.setCreatedTime(new Date());
        modelClassService.saveModelClass(modelClass);
        return ResponseEntity.status(HttpStatus.CREATED).body(modelClass);

    }

    @PutMapping("/{modelClassId}")
    @ApiOperation(value = "修改模型分类", notes = "修改模型分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "modelClassId", dataType = "int", value = "modelClassId"),
            @ApiImplicitParam(paramType = "body", name = "modelClass", dataType = "ModelClass", value = "ModelClass")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = ModelClass.class)})
    public ResponseEntity saveModelClass(@PathVariable Integer modelClassId, @RequestBody ModelClass modelClass) {
        int userId = UserLoginUtils.getUserId();
        modelClass.setModelClassId(modelClassId);
        modelClass.setUpdatedBy(userId);
        modelClass.setUpdatedTime(new Date());
        modelClassService.updateModelClass(modelClass);
        return ResponseEntity.status(HttpStatus.CREATED).body(modelClass);
    }

    @DeleteMapping("/{modelClassId}")
    @ApiOperation(value = "删除模型分类", notes = "删除模型分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "modelClassId", dataType = "int", value = "modelClassId")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity saveModelClass(@PathVariable Integer modelClassId) {
        int userId = UserLoginUtils.getUserId();
        ModelClass modelClass = new ModelClass();
        modelClass.setModelClassId(modelClassId);
        modelClass.setDeleted(true);
        modelClass.setUpdatedTime(new Date());
        modelClass.setUpdatedBy(userId);
        modelClassService.updateModelClass(modelClass);
        return ResponseEntity.ok(true);
    }
}
